package com.fcgzs.springboot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fcgzs.springboot.mapper.CheckgroupMapper;
import com.fcgzs.springboot.mapper.CheckitemMapper;
import com.fcgzs.springboot.mapper.SetmealCheckgroupMapper;
import com.fcgzs.springboot.mapper.SetmealMapper;
import com.fcgzs.springboot.pojo.Checkgroup;
import com.fcgzs.springboot.pojo.Checkitem;
import com.fcgzs.springboot.pojo.Setmeal;
import com.fcgzs.springboot.pojo.SetmealCheckgroup;
import com.fcgzs.springboot.service.SetmealService;
import com.fcgzs.springboot.util.MessageConstant;
import com.fcgzs.springboot.util.PageResult;
import com.fcgzs.springboot.util.QueryPageBean;
import com.fcgzs.springboot.util.Result;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Service
public class SetmealServiceImpl implements SetmealService {

    @Resource
    private SetmealMapper setmealMapper;
    @Resource
    private SetmealCheckgroupMapper setmealCheckgroupMapper;
    @Resource
    private CheckgroupMapper checkgroupMapper;
    @Resource
    private CheckitemMapper checkitemMapper;
    //添加套餐
    @Override
    public Result addSetmealInfo(Setmeal setmeal, Integer[] ids) {
        boolean flag = false;
        //更新套餐
        if(setmeal.getId() != null){
            //删除套餐的中间表数据
            QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
            setmealCheckgroupQueryWrapper.eq("setmeal_id",setmeal.getId());
            flag = setmealCheckgroupMapper.delete(setmealCheckgroupQueryWrapper) > 0;
            flag = setmealMapper.updateById(setmeal) > 0;
        }else {
            flag = setmealMapper.insert(setmeal) > 0;
        }
        for(Integer id : ids){
            SetmealCheckgroup setmealCheckgroup = new SetmealCheckgroup();
            setmealCheckgroup.setSetmealId(setmeal.getId());
            setmealCheckgroup.setCheckgroupId(id); //问题解决：setCheckgroupId错写为setSetmealId
            flag = setmealCheckgroupMapper.insert(setmealCheckgroup) > 0;
        }
        if(flag){
            return new Result(flag, MessageConstant.ADD_SETMEAL_SUCCESS);
        }else{
            return new Result(flag,MessageConstant.ADD_SETMEAL_FAIL);
        }
    }

    //分页方法
    @Override
    public PageResult querySetmealPageInfo(QueryPageBean queryPageBean,Integer mark) {
        if(mark==2){
            Page<Setmeal> page = new Page<>(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
            QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
            if(queryPageBean.getQueryString()!=null&&queryPageBean.getQueryString().length()>0){
                queryWrapper.like("code",queryPageBean.getQueryString());
                queryWrapper.or();
                queryWrapper.like("name",queryPageBean.getQueryString());
                queryWrapper.or();
                queryWrapper.like("helpCode",queryPageBean.getQueryString());
            }
            Page<Setmeal> checkitemPage = setmealMapper.selectPage(page, queryWrapper);

            return new PageResult(checkitemPage.getTotal(),checkitemPage.getRecords());
        }else if(mark==1){
            List<Setmeal> setmeals = setmealMapper.selectList(null);
            return new PageResult(1L,setmeals);
        }
        return null;
    }
    //删除套餐
    @Override
    public Result deleteSetmealInfo(Integer id) {

        //删除图片
        Setmeal setmeal = setmealMapper.selectById(id);
        String path = "E:\\缓存区\\上传路径\\upload\\";
        File file = new File(path + setmeal.getImg());
        file.delete();

        boolean flag = false;
        //删除从表
        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",id);
        //删除关系表中的数据
        flag =  setmealCheckgroupMapper.delete(setmealCheckgroupQueryWrapper)>0;        //删除我们的主表数据
        flag = setmealMapper.deleteById(id)>0;
        if(flag){
            return new Result(flag, MessageConstant.DELETE_SETMEAL_SUCCESS);
        }else{
            return new Result(flag, MessageConstant.DELETE_SETMEAL_FAIL);
        }
    }
    //获取套餐
    @Override
    public Result getOneSetmealById(Integer id) {
        Map<String, Object> map = new HashMap<>();
        Setmeal setmeal = setmealMapper.selectById(id);

        QueryWrapper<SetmealCheckgroup> setmealCheckgroupQueryWrapper = new QueryWrapper<>();
        setmealCheckgroupQueryWrapper.eq("setmeal_id",id);
        List<SetmealCheckgroup> setmealCheckgroups = setmealCheckgroupMapper.selectList(setmealCheckgroupQueryWrapper);
        Integer [] ids = new Integer[setmealCheckgroups.size()];
        int i = 0;
        for(SetmealCheckgroup newOne : setmealCheckgroups){
            ids[i] = newOne.getCheckgroupId();
            i++;
        }
        map.put("oneSetmeal",setmeal);
        map.put("ids",ids);
        return new Result(true,"",map);
    }
    //移动端：展示套餐详情
    @Override
    public Result querySetmealDetail(Integer id) {
        //多表联查：套餐、检查组、检查项
        Setmeal setmeal = setmealMapper.selectById(id);
        List<Checkgroup> checkGoupBySetmealId = checkgroupMapper.getCheckGoupBySetmealId(id);
        for(Checkgroup one: checkGoupBySetmealId){
            List<Checkitem> checkItemByGroupId = checkitemMapper.getCheckItemByGroupId(one.getId());
            one.setCheckItems(checkItemByGroupId);
        }
        setmeal.setCheckGroups(checkGoupBySetmealId);
        return new Result(true,"",setmeal);
    }
}
